package com.playball.lottery.frm.yhzx;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.IdCard;


/**
 * 安全中心 - 个人信息
 * 
 * @author tanglizhong
 * @version 2014-3-26 上午10:37
 */
public class ScPersonActivity extends BaseActivity {

	/**
	 * 个人信息布局
	 */
	private View view_sc_person_info;
	/** 真实姓名 */
	private TextView yhzx_sc_tv_realname;
	/** 身份证号 */
	private TextView yhzx_sc_tv_idcard;

	/**
	 * 个人信息设置布局
	 */
	private View view_sc_person_edit;
	/** 真实姓名 */
	private EditText yhzx_sc_et_realname;
	/** 身份证号 */
	private EditText yhzx_sc_et_idcard;
	/** 立即提交 */
	private Button yhzx_sc_btn_submit;

	/** 用户名 */
	private TextView yhzx_sc_tv_person;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yhzx_sc_person);

		titleBar();
		initView();
	}

	/** 初始化当前账号信息 */
	private void initView() {
		view_sc_person_info = findViewById(R.id.view_sc_person_info);
		view_sc_person_edit = findViewById(R.id.view_sc_person_edit);

		yhzx_sc_tv_person = (TextView) findViewById(R.id.yhzx_sc_tv_person);
		yhzx_sc_tv_realname = (TextView) findViewById(R.id.yhzx_sc_tv_realname);
		yhzx_sc_tv_idcard = (TextView) findViewById(R.id.yhzx_sc_tv_idcard);

		yhzx_sc_et_realname = (EditText) findViewById(R.id.yhzx_sc_et_realname);
		yhzx_sc_et_idcard = (EditText) findViewById(R.id.yhzx_sc_et_idcard);

		yhzx_sc_btn_submit = (Button) findViewById(R.id.yhzx_sc_btn_submit);

		yhzx_sc_tv_person.setText(ApplicationData.getInstance().getUserName());

		// yhzx_sc_tv_realname.setText(ApplicationData.getInstance().getRealName());
		// yhzx_sc_tv_idcard.setText(ApplicationData.getInstance().getIDCardNumber());
		if (!"".equals(ApplicationData.getInstance().getRealName()))
			yhzx_sc_tv_realname.setText(ApplicationData.getInstance().getRealName().substring(0, 1) + "***");
		if (!"".equals(ApplicationData.getInstance().getIDCardNumber()))
			yhzx_sc_tv_idcard.setText(ApplicationData.getInstance().getIDCardNumber().substring(0, 5)
					+ "***"
					+ ApplicationData.getInstance().getIDCardNumber()
							.substring(ApplicationData.getInstance().getIDCardNumber().length() - 3));

		yhzx_sc_et_realname.setText(ApplicationData.getInstance().getRealName());
		yhzx_sc_et_idcard.setText(ApplicationData.getInstance().getIDCardNumber());

		yhzx_sc_btn_submit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				submit();
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (TextUtils.equals(ApplicationData.getInstance().getRealName(), "")
				|| TextUtils.equals(ApplicationData.getInstance().getIDCardNumber(), "")) {
			view_sc_person_edit.setVisibility(View.VISIBLE);
			view_sc_person_info.setVisibility(View.GONE);
		} else {
			view_sc_person_edit.setVisibility(View.GONE);
			view_sc_person_info.setVisibility(View.VISIBLE);
		}
		
	}

	public void onPause() {
		super.onPause();
		
	}

	/** 立即提交点击处理 */
	private void submit() {
		IdCard idCard = new IdCard(yhzx_sc_et_idcard.getText().toString().trim().toUpperCase());
		if (yhzx_sc_et_realname.getText().toString().length() < 2) {
			yhzx_sc_et_realname.setError("请填写您的真实姓名");
		} else if (idCard.isCorrect() != 0) {
			yhzx_sc_et_idcard.setError("请输入您的真实身份证号码");
		} else {
			AsyncLoader task = new AsyncLoader();
			task.execute("102");
		}
	}

	/**
	 * 提交后的异步处理任务
	 */
	private class AsyncLoader extends AsyncTask<String, Integer, String> {
		ShowTipDialog showdialog = new ShowTipDialog();
		String realname;
		String idcard;

		@Override
		protected void onPreExecute() {
			showdialog.showDialog(ScPersonActivity.this);
			realname = yhzx_sc_et_realname.getText().toString();
			idcard = yhzx_sc_et_idcard.getText().toString();
		}

		@Override
		protected String doInBackground(String... params) {
			String action = params[0];
			String strResult;

			try {
				JSONObject param = new JSONObject();
				param.put("RName", realname);
				param.put("IDCard", idcard);

				// 获取请求返回值
				String UserID = ApplicationData.getInstance().getUserID();
				strResult = GetRequestParams.getRequestUserIDResult(param.toString(), UserID, action,
						getApplicationContext());
				return strResult;
			} catch (JSONException e) {
				// e.printStackTrace();
				return null;
			}
		}

		@Override
		protected void onPostExecute(String result) {
			try {
				if (null != result) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (js.getInt("Code") == 0) {
						view_sc_person_edit.setVisibility(View.GONE);
						view_sc_person_info.setVisibility(View.VISIBLE);

						ApplicationData.getInstance().setRealName(realname);
						ApplicationData.getInstance().setIDCardNumber(idcard);
						yhzx_sc_tv_realname
								.setText(ApplicationData.getInstance().getRealName().substring(0, 1) + "***");
						yhzx_sc_tv_idcard.setText(ApplicationData.getInstance().getIDCardNumber().substring(0, 5)
								+ "***"
								+ ApplicationData.getInstance().getIDCardNumber()
										.substring(ApplicationData.getInstance().getIDCardNumber().length() - 3));
					}
					String data = js.getString("Data");
					showTip(data);
				}
			} catch (JSONException e) {
				// e.printStackTrace();
			} finally {
				showdialog.closeDialog();
			}
		}

	}

	/** TitleBar标题栏 */
	public void titleBar() {
		TextView info = (TextView) findViewById(R.id.top_title_info);
		info.setText(R.string.yhzx_sc_person);
		TextView back = (TextView) findViewById(R.id.top_title_back);
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	/** 显示文字提示 */
	private void showTip(String tip) {
		Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
	}
}
